/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.hardcodeando.ui;

import ar.com.hardcodeando.algorithm.BadFormedKeyException;
import ar.com.hardcodeando.algorithm.HillCipher;
import ar.com.hardcodeando.dto.HillDTO;
import ar.com.hardcodeando.ui.utils.AlgorithmStateStorage;
import ar.com.hardcodeando.ui.utils.MyIntFilter;
import java.awt.Color;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.text.PlainDocument;


/**
 *
 * @author Nestor
 */
public class HillEvaluarPanel extends javax.swing.JPanel {

    /**
     * Creates new form HillPanel
     */
    public HillEvaluarPanel() {
        initComponents();

        // Deshabilitamos tabs
        jTabbedPane1.setEnabledAt(1, false);
        jTabbedPane1.setEnabledAt(2, false);
        jTabbedPane1.setEnabledAt(3, false);
        PlainDocument doc = (PlainDocument)jTextField1.getDocument();
        doc.setDocumentFilter(new MyIntFilter());
        doc = (PlainDocument)jTextField2.getDocument();
        doc.setDocumentFilter(new MyIntFilter());
        doc = (PlainDocument)jTextField3.getDocument();
        doc.setDocumentFilter(new MyIntFilter());
        doc = (PlainDocument)jTextField4.getDocument();
        doc.setDocumentFilter(new MyIntFilter());
            
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupPreg1 = new javax.swing.ButtonGroup();
        buttonGroupPreg2 = new javax.swing.ButtonGroup();
        buttonGroupPreg3 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        IntroHillPanel = new javax.swing.JPanel();
        botonContinuar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        rbCorrecto1 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        botonCorregir = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        jRadioButton4 = new javax.swing.JRadioButton();
        rbCorrecto2 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jLabel20 = new javax.swing.JLabel();
        jRadioButton7 = new javax.swing.JRadioButton();
        jRadioButton8 = new javax.swing.JRadioButton();
        rbCorrecto3 = new javax.swing.JRadioButton();
        botonReintentar = new javax.swing.JButton();
        botonResolver = new javax.swing.JButton();
        hillPaso1Panel = new javax.swing.JPanel();
        botonSiguientePaso1 = new javax.swing.JButton();
        lblHeaderPaso1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        botonValidarPaso1 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        botonMostrarPaso1 = new javax.swing.JButton();
        hillPaso2Panel = new javax.swing.JPanel();
        botonSiguientePaso2 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        botonValidarPaso2 = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        botonMostrarPaso2 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        hillPaso3Panel = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextArea3 = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea4 = new javax.swing.JTextArea();
        jButton9 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Hill Cipher - Modo Evaluar");

        jTabbedPane1.setFocusable(false);

        IntroHillPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        botonContinuar.setText("Continuar");
        botonContinuar.setEnabled(false);
        botonContinuar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonContinuarActionPerformed(evt);
            }
        });
        IntroHillPanel.add(botonContinuar, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 470, 100, 30));

        jLabel2.setText("Hill Cipher es un algoritmo de encriptacion basado en algebra lineal, el cual toma el alfabeto de 26 letras como un espacio lineal.");
        IntroHillPanel.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 750, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel4.setText("Introducción");
        IntroHillPanel.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        jLabel18.setText("<html><ul><li>¿Que necesito para desenciptar un mensaje si tengo la matriz de encriptación?</li></ul></html>");
        IntroHillPanel.add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 310, -1, -1));

        buttonGroupPreg1.add(jRadioButton1);
        jRadioButton1.setLabel("Matriz Inversible");
        jRadioButton1.setName(""); // NOI18N
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });
        IntroHillPanel.add(jRadioButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 120, -1, -1));

        buttonGroupPreg1.add(rbCorrecto1);
        rbCorrecto1.setLabel("Matriz Inversible modulo 26");
        IntroHillPanel.add(rbCorrecto1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 140, -1, -1));

        buttonGroupPreg1.add(jRadioButton3);
        jRadioButton3.setLabel("Matriz no inversible");
        IntroHillPanel.add(jRadioButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 160, -1, -1));

        botonCorregir.setText("Corregir");
        botonCorregir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCorregirActionPerformed(evt);
            }
        });
        IntroHillPanel.add(botonCorregir, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 470, 110, 30));

        jLabel19.setText("<html><ul><li>¿Que caracterista tiene que clave para enciptar?</li></ul></html>");
        IntroHillPanel.add(jLabel19, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, -1, -1));

        buttonGroupPreg2.add(jRadioButton4);
        jRadioButton4.setText("Sumatoria");
        IntroHillPanel.add(jRadioButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 230, -1, -1));

        buttonGroupPreg2.add(rbCorrecto2);
        rbCorrecto2.setText("Producto punto");
        IntroHillPanel.add(rbCorrecto2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 270, -1, -1));

        buttonGroupPreg2.add(jRadioButton6);
        jRadioButton6.setText("Division");
        IntroHillPanel.add(jRadioButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 250, -1, -1));

        jLabel20.setText("<html><ul><li>¿Que operación se realiza con el mensaje y la clave para encriptar?</li></ul></html>");
        IntroHillPanel.add(jLabel20, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 200, -1, -1));

        buttonGroupPreg3.add(jRadioButton7);
        jRadioButton7.setText("Realizar el producto de matrices");
        jRadioButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton7ActionPerformed(evt);
            }
        });
        IntroHillPanel.add(jRadioButton7, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 340, -1, -1));

        buttonGroupPreg3.add(jRadioButton8);
        jRadioButton8.setText("Obtener matriz inversa");
        IntroHillPanel.add(jRadioButton8, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 360, -1, -1));

        buttonGroupPreg3.add(rbCorrecto3);
        rbCorrecto3.setText("Obtener la matriz inverza modulo 26");
        IntroHillPanel.add(rbCorrecto3, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 380, -1, -1));

        botonReintentar.setText("Reintentar");
        botonReintentar.setEnabled(false);
        botonReintentar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonReintentarActionPerformed(evt);
            }
        });
        IntroHillPanel.add(botonReintentar, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 470, 110, 30));

        botonResolver.setText("Resolver");
        botonResolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonResolverActionPerformed(evt);
            }
        });
        IntroHillPanel.add(botonResolver, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 470, 110, 30));

        jTabbedPane1.addTab("Introducción", IntroHillPanel);

        hillPaso1Panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        botonSiguientePaso1.setText("Siguiente");
        botonSiguientePaso1.setEnabled(false);
        botonSiguientePaso1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSiguientePaso1ActionPerformed(evt);
            }
        });
        hillPaso1Panel.add(botonSiguientePaso1, new org.netbeans.lib.awtextra.AbsoluteConstraints(641, 470, 100, 30));

        lblHeaderPaso1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lblHeaderPaso1.setText("Paso 1: Obtener una clave");
        hillPaso1Panel.add(lblHeaderPaso1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        jLabel5.setText("<html>La clave es una matriz de nxn la cual tiene que cumplir con lo siguiente para ser valida. <br> Para este ejemplo vamos a utilizar n=2.</html>");
        hillPaso1Panel.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(21, 44, 610, -1));

        jLabel7.setText("<html> <ul> <li>Dicha matriz es la matriz de encriptación cuyos valores se encuentran <br>entre los valores designados del alfabeto.<br><br></li> <li>Esta matriz encriptación debe ser inversible porque su inversa se usara <br>para desencriptar mas adelante.<br><br></li> <li>El determinante de la matriz tiene que ser un primo relativo al tamaño<br> de del alfabeto (26)<br><br></li> </ul> </html>");
        hillPaso1Panel.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(418, 90, 400, 190));

        jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jTextField1.setText("0");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });
        hillPaso1Panel.add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 170, 40, 30));

        jTextField2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jTextField2.setText("0");
        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });
        hillPaso1Panel.add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 170, 40, 30));

        jTextField3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jTextField3.setText("0");
        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });
        hillPaso1Panel.add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 220, 40, 30));

        jTextField4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jTextField4.setText("0");
        jTextField4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField4ActionPerformed(evt);
            }
        });
        hillPaso1Panel.add(jTextField4, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 220, 40, 30));

        jLabel3.setText("Matriz de encriptación 2x2");
        hillPaso1Panel.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 130, -1, 20));

        botonValidarPaso1.setText("Validar");
        botonValidarPaso1.setPreferredSize(new java.awt.Dimension(71, 29));
        botonValidarPaso1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonValidarPaso1ActionPerformed(evt);
            }
        });
        hillPaso1Panel.add(botonValidarPaso1, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 470, 100, 30));

        jButton10.setText("Guardar");
        jButton10.setPreferredSize(new java.awt.Dimension(71, 29));
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        hillPaso1Panel.add(jButton10, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 470, 100, -1));

        botonMostrarPaso1.setLabel("Mostrar");
        botonMostrarPaso1.setPreferredSize(new java.awt.Dimension(71, 29));
        botonMostrarPaso1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                botonMostrarPaso1MousePressed(evt);
            }
        });
        botonMostrarPaso1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonMostrarPaso1ActionPerformed(evt);
            }
        });
        hillPaso1Panel.add(botonMostrarPaso1, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 470, 110, 30));

        jTabbedPane1.addTab("Paso 1 de 3", hillPaso1Panel);

        hillPaso2Panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        botonSiguientePaso2.setText("Siguiente");
        botonSiguientePaso2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSiguientePaso2ActionPerformed(evt);
            }
        });
        hillPaso2Panel.add(botonSiguientePaso2, new org.netbeans.lib.awtextra.AbsoluteConstraints(650, 470, 100, 30));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel8.setText("Paso 2: Vamos a encriptar");
        hillPaso2Panel.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        jLabel9.setText("En esta parte, usted podra ingresar el texto que desee, pero luego tendra que introducir el texto encriptado utilizando la clave ingresada anteriormente");
        hillPaso2Panel.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        jLabel14.setText("Introduzca un mensaje a encriptar:");
        hillPaso2Panel.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 140, -1, -1));

        botonValidarPaso2.setText("Validar");
        botonValidarPaso2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonValidarPaso2ActionPerformed(evt);
            }
        });
        hillPaso2Panel.add(botonValidarPaso2, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 470, 100, 30));

        jLabel13.setText("Introduzca el mensaje encriptado:");
        hillPaso2Panel.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 260, -1, -1));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        hillPaso2Panel.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 140, 380, -1));

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        hillPaso2Panel.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 270, 380, -1));

        botonMostrarPaso2.setLabel("Mostrar");
        botonMostrarPaso2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonMostrarPaso2ActionPerformed(evt);
            }
        });
        hillPaso2Panel.add(botonMostrarPaso2, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 470, 110, 30));

        jButton11.setText("Guardar");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });
        hillPaso2Panel.add(jButton11, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 470, 110, -1));

        jTabbedPane1.addTab("Paso 2 de 3", hillPaso2Panel);

        hillPaso3Panel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel6.setText("Paso 3: Desencriptando");
        hillPaso3Panel.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        jLabel11.setText("En este paso usted prodra validar manualmente el deenciptado");
        hillPaso3Panel.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, -1, -1));

        jLabel15.setText("Introduzca un mensaje a desencriptar:");
        hillPaso3Panel.add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 140, -1, -1));

        jLabel16.setText("Introduzca el mensaje oculto");
        hillPaso3Panel.add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 260, -1, -1));

        jButton6.setText("Validar");
        jButton6.setPreferredSize(new java.awt.Dimension(71, 29));
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        hillPaso3Panel.add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 470, 90, -1));

        jTextArea3.setColumns(20);
        jTextArea3.setRows(5);
        jScrollPane3.setViewportView(jTextArea3);

        hillPaso3Panel.add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 140, 380, -1));

        jTextArea4.setColumns(20);
        jTextArea4.setRows(5);
        jScrollPane4.setViewportView(jTextArea4);

        hillPaso3Panel.add(jScrollPane4, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 260, 380, -1));

        jButton9.setText("Mostrar");
        jButton9.setPreferredSize(new java.awt.Dimension(71, 29));
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        hillPaso3Panel.add(jButton9, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 470, 100, -1));

        jButton12.setText("Guardar");
        jButton12.setPreferredSize(new java.awt.Dimension(71, 29));
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });
        hillPaso3Panel.add(jButton12, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 470, 100, -1));

        jTabbedPane1.addTab("Paso 3 de 3", hillPaso3Panel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 849, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(296, 296, 296)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 339, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 617, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void botonSiguientePaso1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSiguientePaso1ActionPerformed
        jTabbedPane1.setEnabledAt(2, true);
        jTabbedPane1.setSelectedIndex(2);
    }//GEN-LAST:event_botonSiguientePaso1ActionPerformed

    private void botonContinuarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonContinuarActionPerformed
        jTabbedPane1.setEnabledAt(1, true); 
        jTabbedPane1.setSelectedIndex(1);
    }//GEN-LAST:event_botonContinuarActionPerformed

    private void botonSiguientePaso2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSiguientePaso2ActionPerformed
        jTabbedPane1.setEnabledAt(3, true);
        jTabbedPane1.setSelectedIndex(3);
    }//GEN-LAST:event_botonSiguientePaso2ActionPerformed

    private void botonValidarPaso2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonValidarPaso2ActionPerformed
        HillCipher hill = new HillCipher(2);
        String message = jTextArea1.getText();
        String messageCode = jTextArea2.getText();
      
        String msgEncriptado;
        
        int clave[][] = getKeyEntered();
        try {
            msgEncriptado = hill.encrypt(message, clave);
            msgEncriptado = msgEncriptado.toUpperCase();
            if (msgEncriptado.equals(messageCode.trim().toUpperCase())){
                JOptionPane.showMessageDialog(hillPaso2Panel, "Correcto!", TOOL_TIP_TEXT_KEY, WIDTH);
            } else {
                JOptionPane.showMessageDialog(hillPaso2Panel, "Incorrecto", TOOL_TIP_TEXT_KEY, WIDTH);
            }
        } catch (BadFormedKeyException ex) {
            JOptionPane.showMessageDialog(hillPaso2Panel, "La clave es invalida!", "Hill", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_botonValidarPaso2ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        HillCipher hill = new HillCipher(2);
        String message = jTextArea3.getText();
        String messageCode = jTextArea4.getText();
      
        String msgHidden;
        
        int clave[][] = getKeyEntered();
        try {
            int claveInv[][] = hill.calculateKeyInv(clave);
            msgHidden = hill.decoding(message, claveInv);
            msgHidden = msgHidden.toUpperCase();
            if (msgHidden.equals(messageCode.trim().toUpperCase())){
                JOptionPane.showMessageDialog(hillPaso3Panel, "Correcto!", TOOL_TIP_TEXT_KEY, WIDTH);
            } else {
                JOptionPane.showMessageDialog(hillPaso3Panel, "Incorrecto", TOOL_TIP_TEXT_KEY, WIDTH);
            }
        } catch (BadFormedKeyException ex) {
            JOptionPane.showMessageDialog(hillPaso3Panel, "La clave es invalida!", "Hill", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton7ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton7ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField2ActionPerformed

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField3ActionPerformed

    private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField4ActionPerformed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
 
    }//GEN-LAST:event_jTextField1KeyTyped

    public int[][] getKeyEntered() {
        int a=0;int b=0;int c=0;int d=0;
        a = Integer.parseInt(jTextField1.getText());
        b = Integer.parseInt(jTextField2.getText());
        c = Integer.parseInt(jTextField3.getText());
        d = Integer.parseInt(jTextField4.getText());
        int clave[][] = new int[2][2];
        clave[0][0] = a;
        clave[0][1] = b;
        clave[1][0] = c;
        clave[1][1] = d;
        return clave;
    }
    private void botonValidarPaso1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonValidarPaso1ActionPerformed
        HillCipher hillCipher = new HillCipher(2);
        int clave[][] = getKeyEntered();

        if (hillCipher.checkMatriz(clave)) {
            JOptionPane.showMessageDialog(hillPaso1Panel, "La clave es valida", TOOL_TIP_TEXT_KEY, WIDTH);
            botonSiguientePaso1.setEnabled(true);
        } else {
            JOptionPane.showMessageDialog(hillPaso1Panel, "La clave es invalida!", "Hill", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_botonValidarPaso1ActionPerformed

    private void botonMostrarPaso2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonMostrarPaso2ActionPerformed
        HillCipher hill = new HillCipher(2);
        String message = jTextArea1.getText();
      
        String msgEncriptado;
        
        int clave[][] = getKeyEntered();
        try {
            msgEncriptado = hill.encrypt(message, clave);
            jTextArea2.setText(msgEncriptado.toUpperCase());
        } catch (BadFormedKeyException ex) {
            JOptionPane.showMessageDialog(hillPaso2Panel, "La clave es invalida!", "Hill", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_botonMostrarPaso2ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
         HillCipher hill = new HillCipher(2);
        String message = jTextArea3.getText();
     
        String msgHidden;
        
        int clave[][] = getKeyEntered();
        try {
            int claveInv[][] = hill.calculateKeyInv(clave);
            msgHidden = hill.decoding(message, claveInv);
            jTextArea4.setText(msgHidden.toUpperCase());
        } catch (BadFormedKeyException ex) {
            JOptionPane.showMessageDialog(hillPaso3Panel, "La clave es invalida!", "Hill", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButton9ActionPerformed

    private void botonCorregirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCorregirActionPerformed
        short correctas = 0;
        if(this.rbCorrecto1.isSelected()){
            correctas++;
            this.rbCorrecto1.setForeground(Color.green);
        }
        else this.rbCorrecto1.setForeground(Color.red);
        
        if(this.rbCorrecto2.isSelected()){
            correctas++;
            this.rbCorrecto2.setForeground(Color.green);
        }
        else this.rbCorrecto2.setForeground(Color.red);
        
        if(this.rbCorrecto3.isSelected()){
            correctas++;
            this.rbCorrecto3.setForeground(Color.green);
        }
        else this.rbCorrecto3.setForeground(Color.red);
        botonContinuar.setEnabled(true);
        botonReintentar.setEnabled(true);
        JOptionPane.showMessageDialog(null, "Correctas: " + correctas + "/3");
    }//GEN-LAST:event_botonCorregirActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        try {
        JFileChooser saveFile = new JFileChooser();
        int selectOp = saveFile.showSaveDialog(null);
        String path=saveFile.getSelectedFile().getAbsolutePath();
        
        if (selectOp == JFileChooser.APPROVE_OPTION) {
            int a=0;int b=0;int c=0;int d=0;
            a = Integer.parseInt(jTextField1.getText());
            b = Integer.parseInt(jTextField2.getText());
            c = Integer.parseInt(jTextField3.getText());
            d = Integer.parseInt(jTextField4.getText());

            HillDTO hillDTO = new HillDTO();

            hillDTO.setCurrentStep(1);
            hillDTO.setC11(a);
            hillDTO.setC12(b);
            hillDTO.setC21(c);
            hillDTO.setC22(d);


            try {
                AlgorithmStateStorage.saveHill(path, hillDTO);
                JOptionPane.showMessageDialog(null, "El archivo se salvo correctamente");
            } catch (IOException ex) {
                Logger.getLogger(HillEvaluarPanel.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Hubo un error al guardar el archivo");
            }
        }
        } catch (NullPointerException npe) {
            Logger.getLogger(HillEvaluarPanel.class.getName()).log(Level.SEVERE, null, npe);
        }
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        try {
        JFileChooser saveFile = new JFileChooser();
        int selectOp = saveFile.showSaveDialog(null);
        String path=saveFile.getSelectedFile().getAbsolutePath();
        
        if (selectOp == JFileChooser.APPROVE_OPTION) {
            int a=0;int b=0;int c=0;int d=0;
            a = Integer.parseInt(jTextField1.getText());
            b = Integer.parseInt(jTextField2.getText());
            c = Integer.parseInt(jTextField3.getText());
            d = Integer.parseInt(jTextField4.getText());

            HillDTO hillDTO = new HillDTO();

            hillDTO.setCurrentStep(2);
            hillDTO.setC11(a);
            hillDTO.setC12(b);
            hillDTO.setC21(c);
            hillDTO.setC22(d);
            
            hillDTO.setMessage(jTextArea1.getText());
            hillDTO.setCipherMessage(jTextArea2.getText());
            
            try {
                AlgorithmStateStorage.saveHill(path, hillDTO);
                JOptionPane.showMessageDialog(null, "El archivo se salvo correctamente");
            } catch (IOException ex) {
                Logger.getLogger(HillEvaluarPanel.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Hubo un error al guardar el archivo");
            }
        }
        } catch (NullPointerException npe) {
            Logger.getLogger(HillEvaluarPanel.class.getName()).log(Level.SEVERE, null, npe);
        }
    }//GEN-LAST:event_jButton11ActionPerformed

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
        try {
        JFileChooser saveFile = new JFileChooser();
        int selectOp = saveFile.showSaveDialog(null);
        String path=saveFile.getSelectedFile().getAbsolutePath();
        
        if (selectOp == JFileChooser.APPROVE_OPTION) {
            int a=0;int b=0;int c=0;int d=0;
            a = Integer.parseInt(jTextField1.getText());
            b = Integer.parseInt(jTextField2.getText());
            c = Integer.parseInt(jTextField3.getText());
            d = Integer.parseInt(jTextField4.getText());

            HillDTO hillDTO = new HillDTO();

            hillDTO.setCurrentStep(3);
            hillDTO.setC11(a);
            hillDTO.setC12(b);
            hillDTO.setC21(c);
            hillDTO.setC22(d);
            
            hillDTO.setMessage(jTextArea1.getText());
            hillDTO.setCipherMessage(jTextArea2.getText());
         
            hillDTO.setCipherMessage3(jTextArea3.getText());
            hillDTO.setMessage3(jTextArea4.getText());
            try {
                AlgorithmStateStorage.saveHill(path, hillDTO);
                JOptionPane.showMessageDialog(null, "El archivo se salvo correctamente");
            } catch (IOException ex) {
                Logger.getLogger(HillEvaluarPanel.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Hubo un error al guardar el archivo");
            }
        }
        } catch (NullPointerException npe) {
            Logger.getLogger(HillEvaluarPanel.class.getName()).log(Level.SEVERE, null, npe);
        }
    }//GEN-LAST:event_jButton12ActionPerformed

    private void botonMostrarPaso1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonMostrarPaso1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_botonMostrarPaso1ActionPerformed

    private void botonMostrarPaso1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botonMostrarPaso1MousePressed
        this.jTextField1.setText("3");
        this.jTextField2.setText("2");
        this.jTextField3.setText("5");
        this.jTextField4.setText("7");
        botonSiguientePaso1.setEnabled(true);
    }//GEN-LAST:event_botonMostrarPaso1MousePressed

    private void botonReintentarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonReintentarActionPerformed
        this.botonReintentar.setEnabled(false);
        this.botonCorregir.setEnabled(true);
        this.resetearPreguntas();
    }//GEN-LAST:event_botonReintentarActionPerformed

    private void botonResolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonResolverActionPerformed
        rbCorrecto1.setForeground(Color.green);
        rbCorrecto2.setForeground(Color.green);
        rbCorrecto3.setForeground(Color.green);
        botonContinuar.setEnabled(true);
        botonReintentar.setEnabled(true);
    }//GEN-LAST:event_botonResolverActionPerformed

    private void resetearPreguntas(){
        this.rbCorrecto1.setForeground(Color.black);
        this.rbCorrecto2.setForeground(Color.black);
        this.rbCorrecto3.setForeground(Color.black);
        this.buttonGroupPreg1.clearSelection();
        this.buttonGroupPreg2.clearSelection();
        this.buttonGroupPreg3.clearSelection();
        this.botonContinuar.setEnabled(false);
        this.botonCorregir.setEnabled(true);
        this.botonReintentar.setEnabled(false);
        this.botonResolver.setEnabled(true);
    }
    
    public void load(HillDTO hillDTO) {
        
        int step = hillDTO.getCurrentStep();
        
        if (step == 1) {
            this.jTextField1.setText(""+hillDTO.getC11());
            this.jTextField2.setText(""+hillDTO.getC12());
            this.jTextField3.setText(""+hillDTO.getC21());
            this.jTextField4.setText(""+hillDTO.getC22());
            jTabbedPane1.setEnabledAt(1, true);
            jTabbedPane1.setSelectedIndex(1);
        } else if (step == 2) {
            this.jTextField1.setText(""+hillDTO.getC11());
            this.jTextField2.setText(""+hillDTO.getC12());
            this.jTextField3.setText(""+hillDTO.getC21());
            this.jTextField4.setText(""+hillDTO.getC22());
            
            this.jTextArea1.setText(hillDTO.getMessage());
            this.jTextArea2.setText(hillDTO.getCipherMessage());
            jTabbedPane1.setEnabledAt(1, true);
            jTabbedPane1.setEnabledAt(2, true);
            jTabbedPane1.setSelectedIndex(2);
        } else if (step == 3) {
            this.jTextField1.setText(""+hillDTO.getC11());
            this.jTextField2.setText(""+hillDTO.getC12());
            this.jTextField3.setText(""+hillDTO.getC21());
            this.jTextField4.setText(""+hillDTO.getC22());
            
            this.jTextArea1.setText(hillDTO.getMessage());
            this.jTextArea2.setText(hillDTO.getCipherMessage());
            
            this.jTextArea3.setText(hillDTO.getCipherMessage3());
            this.jTextArea4.setText(hillDTO.getMessage3());
            jTabbedPane1.setEnabledAt(1, true);
            jTabbedPane1.setEnabledAt(2, true);
            jTabbedPane1.setEnabledAt(3, true);
            jTabbedPane1.setSelectedIndex(3);
        } else {
            // no corresponde el paso
        }
        
    
    }
    


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel IntroHillPanel;
    private javax.swing.JButton botonContinuar;
    private javax.swing.JButton botonCorregir;
    private javax.swing.JButton botonMostrarPaso1;
    private javax.swing.JButton botonMostrarPaso2;
    private javax.swing.JButton botonReintentar;
    private javax.swing.JButton botonResolver;
    private javax.swing.JButton botonSiguientePaso1;
    private javax.swing.JButton botonSiguientePaso2;
    private javax.swing.JButton botonValidarPaso1;
    private javax.swing.JButton botonValidarPaso2;
    private javax.swing.ButtonGroup buttonGroupPreg1;
    private javax.swing.ButtonGroup buttonGroupPreg2;
    private javax.swing.ButtonGroup buttonGroupPreg3;
    private javax.swing.JPanel hillPaso1Panel;
    private javax.swing.JPanel hillPaso2Panel;
    private javax.swing.JPanel hillPaso3Panel;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JRadioButton jRadioButton7;
    private javax.swing.JRadioButton jRadioButton8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextArea jTextArea3;
    private javax.swing.JTextArea jTextArea4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JLabel lblHeaderPaso1;
    private javax.swing.JRadioButton rbCorrecto1;
    private javax.swing.JRadioButton rbCorrecto2;
    private javax.swing.JRadioButton rbCorrecto3;
    // End of variables declaration//GEN-END:variables

    
}
